package com.example.shengyin.controller;

import com.alibaba.fastjson2.JSON;
import com.example.shengyin.entity.Admin;
import com.example.shengyin.entity.Page;
import com.example.shengyin.entity.SurveyQues;
import com.example.shengyin.service.AdminService;
import com.example.shengyin.service.SurveyQuesService;
import com.example.shengyin.util.Result;
import com.example.shengyin.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * AdminController
 * 
 * @author ruoyi
 * @date 2023-07-29
 */
@RestController
@RequestMapping("/surveyques")
@Validated
@CrossOrigin(origins = "*", maxAge = 3600,allowCredentials="true")
//swagger
@Api(value = "管理员")
//redis缓存用
public class SurveyQuesController
{
    @Autowired
    private SurveyQuesService service;
    @Autowired
    private RedisTemplate redisTemplate;


    public List<Long>  listid(Long stype)
    {
        List<Long> list = service.listid(stype);
        System.out.println(JSON.toJSONString(list));
        return list;
    }
    @GetMapping("/list")
    public Result list(Page page,
                       @ApiParam(name = "title",value = "标题") @RequestParam(required = false) String title,
                       @ApiParam(name = "stype",value = "类别") @RequestParam(required = false) Long stype
    )
    {
        System.out.println("page:"+page+"style:"+stype+","+title);
        List<SurveyQues> list = service.list(page,title,stype);
        int count = service.count(title,stype);
        if(count==0||list==null)
            return Result.success(page,"没有数据");
        page.setData(list);
        page.setCount(count);
        System.out.println(JSON.toJSONString(Result.success(page)));
        return Result.success(page);
    }

    @ApiOperation("单个查找")
    @GetMapping(value = "/select/{id}")
    public Result select(@PathVariable(value = "id") Long id)
    {
        SurveyQues select = service.select(id);
        if(select==null)
            return Result.error("没有找到数据");
        return Result.success(select);
    }
    @ApiOperation("添加管理员")
    @PostMapping("/add")
    public Result insert(@Valid SurveyQues dAdmin, BindingResult bindingResult, HttpServletRequest request)
    {
        System.out.println(request.getHeader("Authorization"));
        Admin admin = (Admin) redisTemplate.opsForValue().get(request.getHeader("Authorization"));
        dAdmin.setPoster(admin.getRealname());
        int insert = service.insert(dAdmin);
        if(insert==0)
            return Result.error("添加失败");
        return Result.success("添加成功");
    }
    @ApiOperation("更新")
    @PostMapping("/edit")
    public Result edit(@Valid SurveyQues dAdmin)
    {
        int insert = service.update(dAdmin);
        if(insert==0)
            return Result.error("修改失败");
        return Result.success("修改成功");
    }
    @ApiOperation("删除单个")
    @GetMapping("/delete/{id}")
    public Result delete(@PathVariable(value = "id")Long id)
    {
        int delete = service.delete(id);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }
    @ApiOperation("删除多个")
	@GetMapping("/deleteByIds/{ids}")
    public Result deleteByIds(@PathVariable(value = "ids")String ids)
    {
        System.out.println("开始这些");
        String s = StrUtil.ListtoString(ids);
        int delete = service.deleteByIds(s);
        if(delete==0)
            return Result.error("删除失败");
        return Result.success("删除成功");
    }
}
